// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bridges.twitter {
	import bridges.storageservice.StorageServiceAccountBase;
	import bridges.storageservice.StorageServiceError;
	
	import events.LoginEvent;
	
	import util.Unpickler;
	
	public class TwitterAccountBase extends StorageServiceAccountBase {
		override protected function Authorize(): void {
			var fnCallbackDone:Function = function(): void {
				dispatchEvent(new LoginEvent(LoginEvent.LOGIN_COMPLETE, true));
				AccountMgr.GetInstance().RefreshUserAttributes(function():void {/*NOP*/}, false); // false == don't save old attrs first
			}
			
			var fnComplete:Function = function(err:Number, strError:String, strCBParams:String=null): void {
				if (err == StorageServiceError.None) {
					var tpa:ThirdPartyAccount = AccountMgr.GetThirdPartyAccount("twitter");
					if (tpa && tpa.storageService) {
						try {
							var obParams:Object = util.Unpickler.loads(strCBParams);
							tpa.storageService.HandleAuthCallback(obParams, fnCallbackDone);
						} catch (e:Error) {
							trace(e);
						}
					}
				}
			}
			
			ClearOtherConnections();
			_tpa.storageService.Authorize(null, fnComplete);
		}
	}
}
